package com.PuzGame.combinenumbers;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.PuzGame.combinenumbers.customViews.ViewHolder;


public class ChallengeMetaDataAdapter extends ArrayAdapter<ChallengeMetaDataItem> {
	protected Context context;
	//protected ArrayList<ChallengeMetaDataItem> items;
	protected ArrayList<MyComparator> Comparators;
	
	protected int CurrentComparator;
	private int LayoutId;
	private String Name;
	public ChallengeMetaDataAdapter(Context c, int layoutId) {
		super (c, layoutId);
		LayoutId = layoutId;
		context = c;
		Comparators = new ArrayList<MyComparator>();
		CurrentComparator = -1;
		
		AddComparator(new ComparatorByName(false));
		AddComparator(new ComparatorByDifficultyLevel(false));
	}
	
	protected void AddComparator(MyComparator comp)
	{
		Comparators.add(comp);
		if(CurrentComparator == -1)
		{
			CurrentComparator = 0;
		}
	}

	public void setItems(ArrayList<ChallengeMetaDataItem> items) {
		this.addAll(items);
		this.notifyDataSetChanged();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder viewHolder = null;

		View List;
		if(convertView==null){
			List=new View(context);
			LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			List=mLayoutinflater.inflate(LayoutId, parent, false);
		}
		else{
			List = (View)convertView;
		}
		
		
		ChallengeMetaDataItem item = ((ChallengeMetaDataItem) getItem(position));
		
		TextView FriendNameTextView = (TextView)List.findViewById(R.id.FriendNameTextView);
		FriendNameTextView.setText(item.GetName());
		
		TextView ChallengeDifficultyTextView = (TextView) List.findViewById(R.id.ChallengeDifficultyTextView);
		
		ChallengeDifficultyTextView.setText(Integer.toString(item.GetDifficultyLevel()));
		
		if(item.getBitmap() == null && item.getImagePath()!=null)
		{
			viewHolder = new ViewHolder();
			
			viewHolder.imageView = (ImageView) List.findViewById(R.id.FriendImage);
	
			viewHolder.imageURL = item.getImagePath();
			
			new DownloadAsyncTask().execute(viewHolder);
		}

		return List;
	}
	
	public static Drawable LoadImageFromWebOperations(String url) {
		
	    try {
	        InputStream is = (InputStream) new URL(url).getContent();
	        Drawable d = Drawable.createFromStream(is, "src name");
	        return d;
	    } catch (Exception e) {
	        return null;
	    }
	}
	
	
	public String GetName()
	{
		return Name;
	}
	

	public Comparator GetCurrentComparator() {
		if(Comparators.size()!=0 && CurrentComparator != -1)
			return Comparators.get(CurrentComparator);
		return null;
	}
	
	public int GetComparatorsCount() {
		if(Comparators.size()!=0)
			return Comparators.size();
		return 0;
	}


	public void SetComparator(int itemId) {
		CurrentComparator = itemId;
		super.sort(Comparators.get(CurrentComparator));
		this.notifyDataSetChanged();
	}

	
	public ArrayList<MyComparator> GetComparators() {
		return Comparators;
	}

	public void RefreshComperator() {
		super.sort(Comparators.get(CurrentComparator));
		this.notifyDataSetChanged();
		
	}
}

